/**
 * Created by yangfan on 2016/12/29.
 */
define(['angular',
    "business/services/constData",
    'jqueryMedia'
],function(angular,constData){
    angular.module("uiComponents",[]).directive("searchBox",function($state,$rootScope,$http){
        return {
            templateUrl:"app/business/services/comComponents/searchBoxComp.html",
            scope:{
                type:"=searchType"
            },
            link:function(scope,element,attrs){
                scope.GLOBALKEYWORD =  $rootScope.GLOBALKEYWORD;
                scope.leaveSearchList =function(){
                    if(scope.isShowData == true){
                        scope.isShowData = false;
                    }
                    scope.isInList = true;
                };
                // 点击资料 显示下拉框
                scope.isShowData = false;
                scope.isInList = false;
                scope.placeholder = "请输入您要搜索的内容";
                //scope.keyWord = "";

                scope.clickData = function(){
                    if(scope.isShowData == false){
                        scope.isShowData = true;
                    }
                };

                scope.searchData =constData.searchItem ;
                scope.curSelectSearchItem = scope.searchData[0];

                //得到当前选择分类
                if($rootScope.routerToStateName.indexOf("search")>-1){
                       var arrTemp  =  scope.searchData.filter(function(item,index){
                        return item.state == $rootScope.routerToStateName ;
                    });
                    scope.curSelectSearchItem = arrTemp[0];
                };
                //初始化标签
                if(scope.type == "big"){
                    getCurLabel(scope.curSelectSearchItem.type);
                }
                scope.selectSearchItem = function(item){
                    scope.curSelectSearchItem = item;
                    if(scope.type == "big"){
                        getCurLabel(scope.curSelectSearchItem.type);
                    }
                    scope.isShowData = false;
                };
                scope.hotSearch = [
                    {title:"房屋结构"},
                    {title:"工程造价"},
                ];
                scope.clickHotSearch = function(item){
                    scope.GLOBALKEYWORD = item.labelName ;
                    scope.search();
                };
                scope.search = function(){
                    $rootScope.GLOBALKEYWORD =  scope.GLOBALKEYWORD;
                    sessionStorage.searchKeyWord = $rootScope.GLOBALKEYWORD;
                    $state.transitionTo(scope.curSelectSearchItem.state, {}, {
                        reload: true, inherit: false, notify: true
                    });
                };
                function getCurLabel(type){
                    $http({
                        method:"POST",
                        url:"http://"+allUrl+":8082/elink_video/api/label/getLabel?type="+ type +"&count="+2,

                    }).success(function(response){
                            scope.curTag = response.data;
                    }).error(function(){})
                }
            }
        }
    })    .directive("uiDialog",function(){
        return {
            restrict: 'EA',
            transclude:true,
            templateUrl:"app/business/services/comComponents/dialog.html",
            scope:{
                conf:"=dialogConf"
            },
            link:function(scope,element,attrs){
                scope.confirm = function(){
                    scope.conf.confirm();
                    scope.conf.isShow = false ;
                };
                scope.textStyle = {
                    "text-align":"center"
                }
                if(scope.conf.hasOwnProperty('align')){
                    if(scope.conf.align == 'left'){
                        scope.textStyle = {
                            "text-align":"left"
                        }
                    }
                }
                scope.cancel = function(){
                    if(typeof scope.conf.cancel == "function"){
                        scope.conf.cancel();
                    }else{
                        scope.conf.isShow =false;
                    }
                };
            }
        }
    }).directive("noContent",function($state){
        return {
            templateUrl:"app/business/services/comComponents/noContent.html",
            scope:{
                type:"=confType",
                content:"=confContent"
            },
            link:function(scope,element,attrs){
                var a = scope;
            }
        }
    })
        .directive('countScroll',function($rootScope){
            return {
                link:function(scope,element,attrs){
                    // 简单的防抖动函数
                    function debounce(func, wait, immediate) {
                        // 定时器变量
                        var timeout;
                        return function() {
                            // 每次触发 scroll handler 时先清除定时器
                            clearTimeout(timeout);
                            // 指定 xx ms 后触发真正想进行的操作 handler
                            timeout = setTimeout(func, wait);
                        };
                    };

                    // 实际想绑定在 scroll 事件上的 handler
                    function realFunc(){
                        console.log("Success");
                    }

                    // 采用了防抖动
                    window.addEventListener('scroll',debounce(realFunc,100));
                    // 没采用防抖动
                    $rootScope.$on('$stateChangeStart',function(){
                        window.removeEventListener('scroll',debounce(realFunc,500));
                    });

                   var leftElement = element.getElementsByClassName('lfbox');

                }
            }
        })
        .directive("dirMedia",function ($sce){
            return {
                scope:{
                    src:"=prevSrc"
                },
                //template:'<iframe  class="f-draw-pdf-container"  ng-src="{{url}}" ></iframe>',
                link:function(scope,element,attrs){
                    element.children().remove();
                    //scope.src = 'http://elink-private.oss-cn-hangzhou.aliyuncs.com/upload%2Ffile%2Ffile%2F20161128%2F824b4418bd4e47de86cca659aca644b7.pdf?OSSAccessKeyId=LTAIBIfDU6YfuXqL&Expires=5086520866&Signature=IT8skCHhUvyiGW1T1GeN5mgqOsg%3D';
                   // scope.src = 'them/images/Wiki.pdf' ;
                    //scope.url = "http://115.29.168.57:8088/?furl=http://"+allUrl+"/upload/file" + scope.src ;
                    scope.url =  $sce.trustAsResourceUrl(scope.src);

                    var appendElement = angular.element('<a class="media" href="'+scope.url+'"></a>');
                    element.append(appendElement);
                    $('.media').media({width: 680, height: 550});

                }
            }
        })
        .directive('scrollSet',function(){
            return {
                scope:{
                    showComp:'=showDialog'
                },
                link:function(scope,element,attrs){
                    $(document).ready(function (){
                        var nScrollHight = 0; //滚动距离总长(注意不是滚动条的长度)
                        var nScrollTop = 0;   //滚动到的当前位置
                        var nDivHight = $(element)[0].scrollHeight;
                        $(element).scroll(function(){
                            nScrollHight = $(this)[0].scrollHeight;
                            nScrollTop = $(this)[0].scrollTop;
                            if(nScrollTop + nDivHight >= nScrollHight){
                                scope.$apply(function(){
                                    scope.showComp = true ;
                                })
                            }

                        });
                    });
                }
            }
        })
    .directive('ngEnter', function () {
        return function (scope, element, attrs) {
            element.bind("keydown keypress", function (event) {
                if(event.which === 13) {
                    scope.$apply(function (){
                        scope.$eval(attrs.ngEnter);
                    });
                    event.preventDefault();
                }
            });
        };
    })
    .filter('notLogo',function(){
        return function(logo){
            if(!logo){
                return 'them/imag/noSrc.png'
            }else{
                return logo
            }
        }
    })
    .factory('setUrl',function(){
        var url = 'http://www.gongchengjie.cn/#';
        //var url = 'http://localhost:3000/elink/index.html#';
        return {
            getUrl :function(stateUrl){
                return url + stateUrl ;
            }
        }
    });
})

